package by.imotaeva.printing_services.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.BindingMaterialDao;
import by.imotaeva.printing_services.datamodel.BindingMaterial;
import by.imotaeva.printing_services.services.BindingMaterialService;

@Service
public class BindingMaterialServiceImpl implements BindingMaterialService{
	private static final Logger LOGGER = LoggerFactory.getLogger(BindingMaterialServiceImpl.class);

	@Inject
	private BindingMaterialDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of BindingMaterialService is created. Class is: {}", getClass().getName());
	}

	@Override
	public BindingMaterial get(Long id) {
		BindingMaterial entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(BindingMaterial bindingMaterial) {
		if (bindingMaterial.getId() == null) {
			LOGGER.debug("Save new: {}", bindingMaterial);
			dao.insert(bindingMaterial);
		} else {
			LOGGER.debug("Update: {}", bindingMaterial);
			dao.update(bindingMaterial);
		}    
		
	}

	@Override
	public void delete(BindingMaterial bindingMaterial) {
		LOGGER.debug("Remove: {}", bindingMaterial);
		dao.delete(bindingMaterial.getId());
		
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all binding-materials");
		dao.deleteAll();
		
	}

}
